<script setup lang="ts">
import { ref } from "vue"
import { $t } from '#/locales'
import { ElMessage } from "element-plus"
import { createGroupOrgApi, updateGroupOrgApi } from '#/api/system/orgMgr';


defineOptions({ name: 'Dialog' })
const inputNTxt = $t('common.inputPhTxt', [$t('orgMgr.orgGroupNameTxt')])
const bindCertTxt = $t('common.selectPhTxt', [$t('orgMgr.bindCertTxt')])

defineProps({
  certListData: {
    type: Array,
    required: true,
  }
});

const titleTxt = {
  plevel: $t('common.newTxt'),
  edit: $t('common.editTxt'),
}

const emit = defineEmits(['queryTable'])
const submitLoading = ref(false)
const dialogVisible = ref(false)
const formRef = ref()
const defaultFormInline = {
  name: null,
  certifiedId: null
}
const formInline = ref({ ...defaultFormInline })
const editData = ref<any>(null)
const handleType = ref<any>('PLEVEL')
const rules = {
  name: [
    { required: true, message: inputNTxt, trigger: 'blur' },
  ],
  certifiedId: [
    { required: true, message: bindCertTxt, trigger: 'change' },
  ]
}

const handleClose = () => {
  formInline.value = { ...defaultFormInline }
  formRef.value.resetFields()
  dialogVisible.value = false
}
const inint = (type: any, data: any) => {
  console.log(type, data)
  editData.value = data ?? null
  handleType.value = type.toLowerCase()
  if (type === 'EDIT') {
    const { name, certifiedId } = data
    formInline.value = { name, certifiedId }
  }
  dialogVisible.value = true
}
const submit = () => {
  if (!formRef) return
  formRef.value?.validate((valid: any) => {
    if (valid) {
      let params = {
        name: formInline.value.name,
        certifiedId: formInline.value.certifiedId
      } as any
      if (handleType.value === 'sublevel') {
        params['pid'] = editData.value.id
        params['certifiedId'] = editData.value.certifiedId
      }
      if (handleType.value === 'edit') {
        params['id'] = editData.value.id
      }
      submitLoading.value = true
      const Api = handleType.value === 'edit' ? updateGroupOrgApi(params) : createGroupOrgApi(params)
      Api.then(() => {
        ElMessage.success($t('common.handleTxt', [$t('common.successTxt')]))
        emit('queryTable')
        handleClose()
      }).finally(() => {
        submitLoading.value = false
      })
    }
  })
}

defineExpose({ inint })

</script>
<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :title="titleTxt[handleType as keyof typeof titleTxt]"
    width="500" :append-to-body="true" :close-on-click-modal="false" :before-close="handleClose">
    <el-form ref="formRef" :model="formInline" :rules="rules" label-width="80px" @submit.prevent>
      <el-form-item :label="$t('orgMgr.orgGroupNameTxt')" prop="name">
        <el-input v-model="formInline.name" :placeholder="$t('common.inputPhTxt', [$t('orgMgr.orgGroupNameTxt')])"
          maxlength="50" />
      </el-form-item>
      <el-form-item v-if="handleType === 'plevel' || (handleType === 'edit' && editData.pid === null)"
        :label="$t('orgMgr.bindCertTxt')" prop="certifiedId">
        <el-select style="width: 13%" class="flex-auto" v-model="formInline.certifiedId" clearable
          :placeholder="bindCertTxt">
          <el-option v-for="item in certListData as any" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">
          {{ $t('common.cancelTxt') }}
        </el-button>
        <el-button type="primary" @click="submit" :loading="submitLoading">
          {{ $t('common.submitTxt') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="less" scoped></style>
